package com.boco.fznq.view;

import java.util.List;

import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.boco.fznq.R;
import com.boco.fznq.model.BannerModel;
import com.culiu.core.utils.debug.DebugLog;
import com.culiu.core.widget.CustomImageView;

/**
 * @describe Page改变事件.
 * @author wangheng
 * @date: 2014年12月8日 上午10:33:21 <br/>
 */
public class OnBannerPageChangeListener implements OnPageChangeListener {
    
    private List<BannerModel> bannerList = null;
    private List<CustomImageView> bannerViewList = null;
    private LinearLayout indicatorContainer = null;
    private BannerViewPager viewPager;
    
    public OnBannerPageChangeListener(BannerViewPager viewPager,LinearLayout indicatorContainer,List<CustomImageView> bannerViewList,List<BannerModel> bannerList){
        this.viewPager = viewPager;
        this.indicatorContainer = indicatorContainer;
        this.bannerViewList = bannerViewList;
        this.bannerList = bannerList;
        viewPager.requestLayout();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(bannerViewList != null && bannerViewList.size() > 1){
            try{
                if(position == bannerViewList.size() - 1 && positionOffset == 0){
                    viewPager.setCurrentItem(1, false);
                }else if(position == 0 && positionOffset == 0){
                    viewPager.setCurrentItem(bannerViewList.size() - 2, false);
                }
            }catch(Exception e){
                DebugLog.e("banner onPageScrolled crash:", e);
            }
        }
    }

    @Override
    public void onPageSelected(int position) {
        if(bannerList == null || bannerList.size() <= 1 || bannerViewList == null || bannerViewList.size() <= 1) {
            return;
        }
        
       /* if(position == 0){
            viewPager.setCurrentItem(bannerViewList.size() - 2,false);
        }else if(position == bannerViewList.size() - 1){
            viewPager.setCurrentItem(1, false);
        }*/

        int count = indicatorContainer.getChildCount();
        int pageIndex = 0;
        if(position == 0){
            pageIndex = count - 1;
        }else if(position == bannerViewList.size() - 1){
            pageIndex = 0;
        }else{
            pageIndex = position - 1;
        }
        for(int i = 0; i < count; i ++ ) {
            ImageView indicatorView = (ImageView)indicatorContainer.getChildAt(i);
            if(i == pageIndex) {
                indicatorView.setImageResource(R.drawable.viewpager_dot_red);
            } else {
                indicatorView.setImageResource(R.drawable.viewpager_dot_white);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
